package awd.bj.jls.webs.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
//import awd.framework.common.datasource.orm.core.param.QueryParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;

import awd.bj.jls.model.TxthModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.DefaultQueryParam;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.bj.jls.webs.utils.TermType;
@Controller
@RequestMapping("/jls_txth")
@RefreshScope
public class TxthController extends BaseAction{

	/**
	 * 
	@GetMapping("/txth")
    public ResponseMessage<PagerResult<TxthModel>> txthQueryForPage(@RequestBody QueryParam param);

    @PostMapping("/txth")
    public ResponseMessage<String> txthSave(@RequestBody TxthModel txthEntity);

    @PutMapping("/txth/{id}")
    public String txthUpdate(@RequestParam(value="id") String id ,@RequestBody TxthModel txthEntity);

	 */
    @Autowired
    private JlsServersApi jlsServersApi;

    /**
     * 初始化模板
     *
     * @param model
     * @return
     */
    @RequestMapping("/index.html")
    public String txthIndex(Model model) {
        User users = this.getCurrentUser();
        model.addAttribute("userName", users.getUsername());
        return "jls_txth/index";
    }

    @RequestMapping("/txth.html")
    public String txth(Model model) {
        User users = this.getCurrentUser();
        model.addAttribute("userName", users.getUsername());
        return "jls_txth/txth";
    }
        @RequestMapping("/txthList")
        @ResponseBody
        public Map<String, Object> txthList(HttpServletRequest request) {
        User users = this.getCurrentUser();
        QueryParam param = new QueryParam();
        String state = request.getParameter("state");
        String kssj=request.getParameter("kssj");
        String jssj=request.getParameter("jssj");
        DefaultQueryParam.addDefaultQueryParam(request, param, state);
        param.and("jsbh",TermType.eq,users.getJsbh());
        ResponseMessage<PagerResult<TxthModel>> list = jlsServersApi.txthQueryForPage(param);
        Map<String, Object> outmap = Maps.newHashMap();
        outmap.put("total", list.getResult().getTotal());
        outmap.put("rows", list.getResult().getData());
        return outmap;
        }
        @RequestMapping("/txthAdd")
        @ResponseBody
        public Result<?> txthAdd(@ModelAttribute TxthModel txthModel) {
        User users = this.getCurrentUser();
        try {
        txthModel.setState("R2");
        txthModel.setJsbh(users.getJsbh());
        txthModel.setCreator(users.getUsername());
        txthModel.setCreatetime(new Date());
        return ResultUtils.success(jlsServersApi.txthSave(txthModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }
        @RequestMapping("/txthDelete")
        @ResponseBody
        public Result<?> txthDelete(@ModelAttribute TxthModel txthModel) {
        User users = this.getCurrentUser();
        try {
        txthModel.setState("R3");
        txthModel.setJsbh(users.getJsbh());
        txthModel.setUpdator(users.getUsername());
        txthModel.setUpdatetime(new Date());
        return ResultUtils.success(jlsServersApi.txthUpdate(txthModel.getId(),txthModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }
        @RequestMapping("/txthUpdate")
        @ResponseBody
        public Result<?> txthUpdate(@ModelAttribute TxthModel txthModel) {
        User users = this.getCurrentUser();
        try {
        txthModel.setState("R2");
        txthModel.setJsbh(users.getJsbh());
        txthModel.setUpdator(users.getUsername());
        txthModel.setUpdatetime(new Date());
        return ResultUtils.success(jlsServersApi.txthUpdate(txthModel.getId(),txthModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }

        
       
}
